package com.example.familylibrary.controller;

import com.example.familylibrary.dto.ReadingProgressDTO;
import com.example.familylibrary.service.ReadingProgressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/progress")
public class ReadingProgressController {

    @Autowired
    private ReadingProgressService readingProgressService;

    @PostMapping
    public ResponseEntity<Map<String, Object>> createOrUpdateProgress(@RequestBody ReadingProgressDTO readingProgressDTO) {
        try {
            ReadingProgressDTO progress = readingProgressService.createOrUpdateProgress(readingProgressDTO);
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "阅读进度更新成功");
            response.put("data", progress);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    @GetMapping("/{bookId}")
    public ResponseEntity<Map<String, Object>> getProgressByUserAndBook(
            @RequestParam Long userId,
            @PathVariable Long bookId) {
        try {
            ReadingProgressDTO progress = readingProgressService.getProgressByUserAndBook(userId, bookId);
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("data", progress);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    @GetMapping
    public ResponseEntity<Map<String, Object>> getProgressByUserId(@RequestParam Long userId) {
        List<ReadingProgressDTO> progressList = readingProgressService.getProgressByUserId(userId);
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("data", progressList);
        return ResponseEntity.ok(response);
    }

    @PutMapping("/{id}/status")
    public ResponseEntity<Map<String, Object>> updateProgressStatus(
            @PathVariable Long id,
            @RequestParam Integer status) {
        try {
            ReadingProgressDTO progress = readingProgressService.updateProgressStatus(id, status);
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "阅读状态更新成功");
            response.put("data", progress);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
}